/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufg.inf.pos.bd;

import br.ufg.inf.pos.br.utils.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author luan
 */
public class StatusDaoImpl implements StatusDao{
   
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;

    private final String COLUMN_TIPOSITUACAO = "TIPOSITUACAO";
    private final String COLUMN_DATASITUACAO = "DATASITUACAO";
    private final String COLUMN_IDSITUACAO = "IDSITUACAO";

    
    @Override
    public int salvar(Status status, Connection conn) {
  
    int codigoInserido = 0;
  
        try {
             preparedStatement = conn
                      .prepareStatement("INSERT into situacao ("+COLUMN_DATASITUACAO+","+COLUMN_TIPOSITUACAO+")"
                            + " values (TO_DATE(?, 'DD.MM.YYYY'),?)",new String[]{COLUMN_IDSITUACAO});
            preparedStatement.setString(1, converterData(status.getDataCadastro()));
            preparedStatement.setString(2, status.getTipoStatus().getStatus());
           
            if (preparedStatement.executeUpdate() > 0) {
                resultSet = preparedStatement.getGeneratedKeys();
                if (null != resultSet && resultSet.next()) {
                    codigoInserido = resultSet.getInt(1);
                }
            }
        } catch (SQLException ex) {
            conn.rollback();
            throw new RuntimeException("Erro ao recuperar objeto: "
                    + ex.getMessage());
        } finally {
           
            return codigoInserido;
        }
    }
    
     public String converterData(Date data){
         SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        String dataConvertida = formatter.format(data);
       return dataConvertida;
    }
}
